package com.rfid.transport;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class Transport_Bluetooth implements IComm {
    private OutputStream os = null;
    private InputStream is = null;
    private BluetoothDevice _device = null;
    private BluetoothSocket _socket = null;
    private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
    private static final String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";

    public int Connect(String conStr) {
        if (this._bluetooth == null) return -2;
        if (!this._bluetooth.enable()) return -2;
        DisConnect();
        this._device = this._bluetooth.getRemoteDevice(conStr);
        try {
            this._socket = this._device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            if (this._socket == null) return 2;
        } catch (IOException e) {
            return -2;
        }
        try {
            this._socket.connect();
        } catch (IOException e) {
            try {
                this._socket.close();
            } catch (IOException iOException) {
            }
            return -2;
        }
        try {
            this.os = this._socket.getOutputStream();
            this.is = this._socket.getInputStream();
        } catch (Exception e) {
            if (this.os != null) try {
                this.os.close();
            } catch (IOException iOException) {
            }
            if (this.is != null) try {
                this.is.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return 0;
    }

    public int Send(byte[] buffer, int offset, int m_count) {
        try {
            this.os.write(buffer, offset, m_count);
        } catch (Exception e) {
            return -2;
        }
        return 0;
    }

    public int Receive(byte[] bufferRead, int LenToRecv) {
        byte[] buffer = new byte[LenToRecv];
        int m_numRead = 0;
        try {
            if (this.is.available() == 0) return 0;
        } catch (IOException e) {
            return -2;
        }
        try {
            m_numRead = this.is.read(buffer);
        } catch (IOException e) {
            return -2;
        }
        if (m_numRead > 0) System.arraycopy(buffer, 0, bufferRead, 0, m_numRead);
        return m_numRead;
    }

    public int DisConnect() {
        if (this._socket == null) return 0;
        try {
            this._socket.close();
        } catch (Exception e) {
            return -2;
        }
        try {
            this.os.close();
        } catch (Exception exception) {
        }
        this.os = null;
        this._socket = null;
        return 0;
    }

    public void ResetInputStream() {
        byte[] bufferRead;
        if (this.is == null) return;
        try {
            this.is.reset();
        } catch (IOException ex) {
        }
        do bufferRead = new byte[255]; while (Receive(bufferRead, bufferRead.length) > 0);
    }
}
